﻿using WzhAddon;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using UnityEngine;
namespace WzhStudioVMDPlay
{
    public class StudioVMDPlayGUI : BaseMgr<StudioVMDPlayGUI>
    {
        private Dictionary<string, List<string>> PagListDic = new Dictionary<string, List<string>>();
        private List<string> pagList = new List<string> { };  // 分页
        private List<string> pagNameList = new List<string> { };  // 分页名称
        private GUIStyle pagStyle;
        private int pagIndex = -1;

        private List<string> musicList = new List<string> { }; // 音乐名称列表
        private List<string> musicPathList = new List<string> { }; // 音乐路径列表
        private string isPlayingMusicName = "None";
        private int selectedMusicIndex = -1; // 当前选中的音乐索引
        private bool isPlaying = false; // 播放状态
        private bool isLooping = false; // 循环模式
        private bool isCamera = false; // 相机模式
        private Rect windowRect = new Rect(20, 20, 400, 500); // 增大面板的初始位置和大小
        private Vector2 Size0 = new Vector2(500, 650);
        private Vector2 Size1 = new Vector2(50, 50);
        private GUIStyle buttonStyle;
        private GUIStyle labelStyle;
        private GUIStyle speedLabelStyle;
        private Vector2 scrollPosition; // 滚动视图的位置

        private int windowID = 0;
        private float PlaySpeed = 1;

        private bool IsInit = false;

        public Func<bool> AddVmdCallBack;
        public Action<List<string>, string, string> LoadVMDCallBack;
        public Action PlayCallBack;
        public Action PauseCallBack;
        public Action StopCallBack;
        public void InItGUI(string pagPath)
        {
            if (string.IsNullOrEmpty(pagPath)) return;
            //string pagPath = "E:\\UnityProject2021\\xqzd_w\\Unity\\Proj_xqzd\\Assets";
            WLog.Log("path: " + pagPath);
        

            // 验证路径是否存在
            if (Directory.Exists(pagPath))
            {
              
                // 获取所有子文件夹路径
                string[] subdirectories = Directory.GetDirectories(pagPath);

                string pagName = "";
                // 打印子文件夹路径
                foreach (string subdirectory in subdirectories)
                {
                    pagName = Path.GetFileName(subdirectory);
                    string[] subdirectories11 = Directory.GetDirectories(subdirectory);
                    PagListDic.Add(pagName, new List<string>(subdirectories11));
                    pagNameList.Add(pagName);
                    pagList.Add(pagName[0].ToString());
                }

                WLog.Log("存在 path: " + pagNameList.Count);
            }
            else
            {
                WLog.Log("不存在 path: " + pagPath);
            }

            OnPagClick(0);

            IsInit = true;
        }
 
        private void OnGUI()
        {
            if (IsInit == false) return;

            if (buttonStyle == null)
            {
                // 初始化样式
                buttonStyle = new GUIStyle(GUI.skin.button);
                buttonStyle.fontSize = 20; // 设置按钮字体大小

                pagStyle = new GUIStyle(GUI.skin.button);
                pagStyle.fontSize = 20;

                labelStyle = new GUIStyle(GUI.skin.label);
                labelStyle.fontSize = 24; // 设置标签字体大小

                speedLabelStyle = new GUIStyle(GUI.skin.label);
                speedLabelStyle.fontSize = 15; // 设置标签字体大小
            }

            if (windowID > 0)
            {
                windowRect.width = Size1.x;
                windowRect.height = Size1.y;
            }
            else
            {
                windowRect.width = Size0.x;
                windowRect.height = Size0.y;
            }
            windowRect = GUI.Window(windowID, windowRect, DrawWindow, "Music Player");
        }

        private void DrawWindow(int wId)
        {
            if (wId > 0)
            {
                // 开按钮
                if (GUILayout.Button("口", buttonStyle))
                {
                    windowID = 0;
                }
                GUI.DragWindow();
                return;
            }
            // 显示音乐列表
            GUILayout.BeginVertical("box");
            // GUILayout.Label("Music List", labelStyle);
            // 显示当前播放的音乐名称
            GUILayout.Label("Playing: " + isPlayingMusicName, labelStyle);
            scrollPosition = GUILayout.BeginScrollView(scrollPosition, GUILayout.Height(450));
            for (int i = 0; i < musicList.Count; i++)
            {
                if (GUILayout.Button(musicList[i], buttonStyle))
                {
                    OnVMDDic(i);  // 点击切换音乐
                }
            }
            GUILayout.EndScrollView();
            GUILayout.EndVertical();

            // 播放/暂停和循环模式按钮在同一行
            GUILayout.BeginHorizontal();
            if (GUILayout.Button("Camera: " + (isCamera ? "On" : "Off"), buttonStyle))
            {
                isCamera = !isCamera;
            }
            if (GUILayout.Button("Loop: " + (isLooping ? "On" : "Off"), buttonStyle))
            {
                isLooping = !isLooping;
            }
            if (GUILayout.Button("Play: " + (isPlaying ? "On" : "Off"), buttonStyle))
            {
                PlayMusic();
            }


            GUILayout.EndHorizontal();
            GUILayout.BeginHorizontal();
            // 添加音量调节滑块
            GUILayout.Label("Speed", speedLabelStyle);
            OnPlaySpeed(GUILayout.HorizontalSlider(PlaySpeed, 0.0f, 3.0f));
            GUILayout.EndHorizontal();

            // pag 在一行
            GUILayout.BeginHorizontal();
            for (int i = 0; i < pagList.Count; i++)
            {
                if (GUILayout.Button(pagList[i], pagStyle))
                {
                    OnPagClick(i);
                }
            }
            GUILayout.EndHorizontal();

            GUILayout.BeginHorizontal();
            // 关闭按钮
            if (GUILayout.Button("Close", buttonStyle))
            {
                windowID = 1;
            }
            // 添加组件按钮
            if (GUILayout.Button("Add", buttonStyle))
            {
                OnAddVmdClick();
            }
            GUILayout.EndHorizontal();
            // 使窗口可拖动
            GUI.DragWindow();
        }

        private void OnPagClick(int index)
        {
            if (index < 0 || index >= pagNameList.Count) return;
            if (pagIndex == index) return;
            musicList.Clear();
            musicPathList.Clear();
            pagIndex = index;
            string pagName = pagNameList[pagIndex];
            foreach (var music in PagListDic[pagName])
            {
                musicPathList.Add(music);
                musicList.Add(Path.GetFileName(music));
            }

            selectedMusicIndex = 0;
        }

        private void OnVMDDic(int index)
        {
            if (index < 0 || index >= musicList.Count) return;
            isPlaying = true;
            index = index % musicList.Count;
            isPlayingMusicName = musicList[index];
            LoadVMD(musicPathList[index]);

            selectedMusicIndex = index;
        }

        private void PlayMusic()
        {
            isPlaying = !isPlaying;
            if (isPlaying)
            {
                // 播放
                if (PlayCallBack == null) return;
                PlayCallBack.Invoke();
            }
            else
            {
                // 暂停vmd
                if (PauseCallBack == null) return;
                PauseCallBack.Invoke();
            }
        }


        private List<string> vmdPathList = new List<string> { "motion.vmd", "motion1.vmd", "motion2.vmd", "motion3.vmd", "motion4.vmd", "motion5.vmd", "motion6.vmd", "motion7.vmd", "motion8.vmd", "motion9.vmd" };
        private string currentVMDPath = "";
        private void LoadVMD(string vmdPath)
        {
            if (currentVMDPath == vmdPath) return;  // 如果当前播放的vmd路径和即将播放的vmd路径相同，则不进行播放
            currentVMDPath = vmdPath;
            string vmdPathRoot = vmdPath + "\\";
            List<string> tpVmdPathList = new List<string> { };

            for (int i = 0; i < vmdPathList.Count; i++)
            {
                string vmdpath = vmdPathRoot + vmdPathList[i];
                if (File.Exists(vmdpath) == false) continue;
                tpVmdPathList.Add(vmdpath);
            }
            string cameraPath = vmdPathRoot + "camera.vmd";
            if (File.Exists(cameraPath) == false || isCamera == false) cameraPath = null;

            string bgmPath = null;
            string[] wavFiles = Directory.GetFiles(vmdPathRoot, "*.wav", SearchOption.AllDirectories);
            if (wavFiles != null && wavFiles.Length > 0) bgmPath = wavFiles[0];

            foreach (var item in tpVmdPathList)
                WLog.Log("LoadVMD: " + item);

            WLog.Log("Loadcamera: " + cameraPath);
            WLog.Log("LoadbgmPath: " + bgmPath);
            // 播放vmd
            if (LoadVMDCallBack != null)
                LoadVMDCallBack.Invoke(tpVmdPathList, cameraPath, bgmPath);
        
        }

        private void PlayLoopVMD()
        {
            currentVMDPath = "";
            //// 循环播放vmd
            //if (isLooping)
            //{
            //    PlayMusic(true, selectedMusicIndex);
            //}
            //else
            //{
            //    PlayMusic(true, selectedMusicIndex + 1);
            //}
        }
        private void OnPlaySpeed(float speed)
        {
            if (PlaySpeed == speed) return;
            PlaySpeed = speed;
            //改变速度
            WLog.Log("PlaySpeed: " + PlaySpeed);
        }

        private void OnAddVmdClick()
        {
            // 添加组件
            if (AddVmdCallBack == null) return;
            bool addres = AddVmdCallBack.Invoke();
            WLog.Log("AddVmdCallBack: "+ addres.ToString());
        }
    }
}
